define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $(".btn-add").data('area',["100%",'100%']);
            $(".btn-edit").data('area',["100%",'100%']);
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'sales/retreat/index' + location.search,
                    add_url: 'sales/retreat/add',
                    // edit_url: 'sales/retreat/edit',
                    // del_url: 'sales/retreat/del',
                    // multi_url: 'sales/retreat/multi',
                    table: 'sales_retreat',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                rowStyle:function(row, index){
                    if (row.ystatus == 5) {
                        return{
                            css:{
                                color:'red'
                            }
                        }
                    }else{
                        return{}
                    }
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),class:'retreat_id'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'number', title: __('Number')},
                        {field: 'salesslip.number', title: __('Salesslip.number')},
                        {field: 'storehouse.name', title: __('Storehouse.name'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=1")},
                        {field: 'collectiontime', title: __('Collectiontime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'abstract', title: __('Abstract')},
                        {field: 'operator', title: __('Operator')},
                        {field: 'company', title: __('Company')},
                        {field: 'tax', title: __('Tax'), operate:'BETWEEN'},
                        {field: 'salescheckout.number', title: __('Salescheckout.number')},
                        {field: 'customer.company', title: __('Customer.nickname')},
                        {field: 'status', title: __('Status'),searchList: $.getJSON(Config.this_url+"/config/zaxconfig?states=11")},
                        {field: 'explain', title: __('Explain'),operate:false,class: 'explain',cellStyle:function (value, row, index) {
                                return {
                                    css: {
                                        "min-width": "100px",
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "120px"
                                    }
                                }
                            }},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'examine',
                                    title: __('退货单审核'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-random',
                                    hidden:function(row){
                                        if(row.ystatus != 1){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var retreat_id =$(this).parents('tr').find('.retreat_id').html()

                                        var url = Config.this_url+'/sales/retreat/detail?ids='+retreat_id
                                        // console.log(url)
                                        layer.open({
                                            type: 2, // 1-在content中写html代码， 2-在content中写src路径
                                            area: ['80%', '90%'],
                                            title: "退货详情",
                                            content: url,
                                            // content: Config.this_url+'/purchase/requisition',
                                        })
                                    }},
                                {
                                    name: 'enter',
                                    title: __('入库'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-level-down',
                                    hidden:function(row){
                                        if(row.ystatus != 2){
                                            return true;
                                        }
                                    },
                                    click: function(data){
                                        var retreat_id =$(this).parents('tr').find('.retreat_id').html()
                                        layer.confirm('是否已经入库', {
                                            time: 20000, //20s后自动关闭
                                            btn: ['是的', '还没']
                                        },function () {

                                            var url = Config.this_url+'/sales/retreat/enter?ids='+retreat_id
                                            $.post(url,{status:3},function (res) {
                                                layer.msg(res.msg)
                                                if(res.code == 1){
                                                    $('.btn-refresh').click()
                                                }
                                            })

                                        },function () {

                                        });
                                    }},
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true)
                                // $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                                // console.log(row)
                                if(row['ystatus'] == 1  && row['salesslip']['number'] == null){
                                        // $(table).data("operate-edit", null);
                                    // $(table).data("operate-edit", null);// 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                                }else{
                                    $(table).data("operate-edit", null);// 列表页面隐藏 .编辑operate-edit - 删除按钮operate-del
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });
            $('body').on('mouseenter','.explain',function () {
                if (this.offsetWidth < this.scrollWidth) {
                    var that = this;
                    var text = $(this).text();
                    layer.tips(text, that,{
                        tips: 1,
                        time: 2000         //设置显示时间
                    });
                }
            })
            $('table').on('dblclick','td',function(){
                if($(this).parents('tr').find('.btn-editone').length >0){
                    return  false;
                }
                var retreat_id = $(this).parents('tr').find('.retreat_id').html()
                var url = Config.this_url+'/sales/retreat/detail?ids='+retreat_id
                // console.log(url)
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['80%', '90%'],
                    title: "退货详情",
                    content: url,
                    // content: Config.this_url+'/purchase/requisition',
                })
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(".btn-add").click(function () {
                if($('#c-storehouse_id').val()== 0 ){
                    layer.msg('未选中出库仓库')
                    return false;
                }
                // if($('#c-customer_id').val()== 0 ){
                //     layer.msg('未选中客户')
                //     return false;
                // }
                layer.open({
                    type: 2, // 1-在content中写html代码， 2-在content中写src路径
                    area: ['70%', '90%'],
                    title: "选择商品",
                    content: 'seach?house_id='+$('#c-storehouse_id').val()+'&customer_id='+$('#c-customer_id').val(),
                })
            })
            Controller.api.bindevent();
        },
        seach:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/specs',
                    table: '',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                pageSize:15,
                // searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        // {checkbox: true,operate:false},
                        {field: 'id', title: 'ID', operate: false,class:'sid'},
                        {field: 'number', title: __('number'),sortable:true},
                        {field: 'title', title: __('title'),  operate: false,sortable:true},
                        // {field: 'place_origin', title: __('place_origin'), formatter: Table.api.formatter.search},

                        // {field: 'unit', title: __('unit'), align: 'left'},
                        {field: 'specs', title: __('规格')},
                        {field: 'stock', title: __('库存'),sortable:true},
                        {field: 'createtime', title: __('Create time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'returnorder',
                                    title: __('选中'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-fighter-jet',
                                    hidden:function(row){
                                        // if(row.status != '审核'){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var sid =$(this).parents('tr').find('.sid').html()
                                        $(this).attr('style','display:none')
                                        parent.getrequisition(sid);
                                        // console.log(parent.getrequisition(sid))
                                        // console.log('返回结果:'+res)
                                    }}
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){
                var sid =$(this).parents('tr').find('.sid').html()
                parent.getrequisition(sid);
                var index = parent.Layer.getFrameIndex(window.name);
                parent.Layer.close(index);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        seach1:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'sales/retreat/seach',
                    table: '',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                pageSize:15,
                // searchFormVisible: true,
                searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        // {checkbox: true,operate:false},
                        {field: 'id', title: 'ID', operate: false,class:'sid'},
                        {field: 'title', title: __('title'),  operate: false,sortable:true},
                        // {field: 'place_origin', title: __('place_origin'), formatter: Table.api.formatter.search},

                        // {field: 'unit', title: __('unit'), align: 'left'},
                        {field: 'specs', title: __('规格')},
                        // {field: 'createtime', title: __('Create time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'returnorder',
                                    title: __('选中'),
                                    classname: 'btn btn-xs btn-success btn-click editstatus',
                                    icon: 'fa fa-fighter-jet',
                                    hidden:function(row){
                                        // if(row.status != '审核'){
                                        //     return true;
                                        // }
                                    },
                                    click: function(data){
                                        var sid =$(this).parents('tr').find('.sid').html()
                                        $(this).attr('style','display:none')
                                        parent.getrequisition(sid);
                                        // console.log(parent.getrequisition(sid))
                                        // console.log('返回结果:'+res)
                                    }}
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            $('table').on('dblclick','td',function(){
                var sid =$(this).parents('tr').find('.sid').html()
                parent.getrequisition(sid);
                var index = parent.Layer.getFrameIndex(window.name);
                parent.Layer.close(index);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        detail: function () {
            $('.cancelbill').click(function () {
                var url =  Config.this_url+'/sales/retreat/cancelbill'+location.search
                layer.prompt({title: '输入红冲原因', formType: 2}, function(pass, index){
                    if(pass == ''){
                        layer.msg('不能为空');
                        return false;
                    }
                    $.post(url,{reason:pass},function (res) {
                        layer.msg(res.msg)
                        if(res.code == 1){
                            layer.close(index);
                            setTimeout(function () {
                                location.reload();
                            }, 800);
                            window.parent.$(".toolbar .btn-refresh").trigger('click');
                        }
                    })
                });
            })
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});